function getBase64Image(img) {
        var canvas = document.createElement("canvas");
        canvas.width = img.width;
        canvas.height = img.height;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, img.width, img.height);
        var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();
        var dataURL = canvas.toDataURL("image/"+ext);
        return dataURL;
}
function createImage(url) {
	var image = new Image();
	image.src = url;
	image.onload = function(){
	    var base64 = getBase64Image(image);
	    x0=document.getElementById("container-spsp").cloneNode(true);
        x0.getElementsByTagName("span")[0].style.display="block";
        x=x0.getElementsByTagName("img")[0];
        y=document.getElementById("container-my");
        x.src = base64; 
        x.setAttribute("class", "add-img"); ;
        y.appendChild(x0); 

        $(".container-img-delete .glyphicon-remove").on("click",function(){

            $(this).parent().remove();
         
        })
	}
}
function showPreview(source) { 
        var container=document.getElementById("container-my");
        var length=container.getElementsByTagName('img').length;
        if(length<=6&&(length+source.files.length)<=6)
        {
          
           
           for(i=0;i<source.files.length;i++)
           { 

             var file=source.files[i];
             if(window.FileReader) {  
                 var fr = new FileReader();  
                 fr.readAsDataURL(file);  
                 fr.onloadend = function(e) { 

                     x0=document.getElementById("container-spsp").cloneNode(true);
                     x0.getElementsByTagName("span")[0].style.display="block";
                     x=x0.getElementsByTagName("img")[0];
                     y=document.getElementById("container-my");
                     x.src = e.target.result; 
                     x.setAttribute("class", "add-img"); ;
                     y.appendChild(x0); 

                     $(".container-img-delete .glyphicon-remove").on("click",function(){

                         $(this).parent().remove();
                      
                     })
                 };  
              } 


           }


           
        }
        else{
            alert("最多上传5张描述图片");
        }
        
    } 

define(
	[
	appBasePath + '/scripts/module.js',
	],
	function (substanceModule) {

    'use strict';

    substanceModule.controller('Substance.GoodsEditController', [
        '$scope',
        '$state',
        '$stateParams',
        'gbits.websns.substance.config',     
        'Substance.GoodsService',
        '$timeout',
        function($scope, $state, $stateParams, config, goodsService, $timeout) {
        	$scope.basePath = config.basePath;         	
        	$scope.model = {
    			id:$stateParams.id,
        		content :''
        	};
        	
        	$('#submitDate').datetimepicker({
                format: 'yyyy-mm-dd',
                minView: "month",
                autoclose:true //选择日期后自动关闭  //选择日期后，不会再跳转去选择时分秒 
               });
        	$('#expiredDate').datetimepicker({
                format: 'yyyy-mm-dd',
                minView: "month",
                autoclose:true //选择日期后自动关闭  //选择日期后，不会再跳转去选择时分秒 

               });
        	
        	
        	$scope.update = function(valid) {
        		if (!valid) {
        			alert("验证失败");
        			return false;
        		}
        		var images = $('.img-container .add-img');
        		if (images.length == 0) {
        			alert('至少一张图片');
        			return false;
        		}
        		var postData = {file_length: images.length};
        		images.each(function(i) {
        			var image = images[i];
        			postData['file_' + i] = $(image).attr('src');
        		});
        		postData = $.extend(postData, $scope.goods);
        		goodsService.update($scope.goods.id, postData).then(function(data) {
        			if (data && data.success) {
        				// goodsService.upload(data.data.id, $scope.addFiles);
        				// 提交
        				$state.go('kernel.appList.substance.goodslist');
        			} else {
        				$scope.errorMsg(data);
        			}  			
        		});
        	};
        	
        	function loadData() {        		
        		goodsService.get($scope.model.id).then(function(data) {
        			if (data) {
        				$scope.goods = data; 
        				$timeout(function(){
        					var images = $scope.goods.goodImages;
        					for (var i in images) {
        						var image = images[i];
        						createImage('images/' + image.imageName);
        					}
        				});
        			}
        		});
        	}
        	
        	loadData();
        }
    ]);
});